﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TheOutfield.UmbExt.UniversalMediaPicker.Controls;
using TheOutfield.UmbExt.UniversalMediaPicker.Extensions;

namespace TheOutfield.UmbExt.UniversalMediaPicker.Providers.SlideShare
{
    public class SlideShareConfigControl : AbstractConfigControl
    {
        private SlideShareConfig _config;
        private TextBox apiKeyTextBox = new TextBox();
        private TextBox sharedSecretTextBox = new TextBox();
        private TextBox usernameTextBox = new TextBox();
        private TextBox passwordTextBox = new TextBox();

        public override string Value
        {
            get
            {
                EnsureChildControls();

                return new SlideShareConfig
                {
                    ApiKey = apiKeyTextBox.Text,
                    SharedSecret = sharedSecretTextBox.Text,
                    Username = usernameTextBox.Text,
                    Password = passwordTextBox.Text != "" ? passwordTextBox.Text : _config.Password
                }
                .SerializeToJson();
            }
        }

        public SlideShareConfigControl(string config)
        {
            _config = config.DeserializeJsonTo<SlideShareConfig>();
        }

        protected override void OnLoad(EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                apiKeyTextBox.Text = _config.ApiKey;
                sharedSecretTextBox.Text = _config.SharedSecret;
                usernameTextBox.Text = _config.Username;
            }

            base.OnLoad(e);
        }

        protected override void CreateChildControls()
        {
            apiKeyTextBox.ID = "apiKeyTextBox";
            sharedSecretTextBox.ID = "sharedSecretTextBox";
            usernameTextBox.ID = "usernameTextBox";
            passwordTextBox.ID = "passwordTextBox";

            apiKeyTextBox.CssClass = "guiInputText guiInputStandardSize";
            sharedSecretTextBox.CssClass = "guiInputText guiInputStandardSize";
            usernameTextBox.CssClass = "guiInputText guiInputStandardSize";
            passwordTextBox.CssClass = "guiInputText guiInputStandardSize";

            passwordTextBox.TextMode = TextBoxMode.Password;

            Controls.Add(apiKeyTextBox);
            Controls.Add(sharedSecretTextBox);
            Controls.Add(usernameTextBox);
            Controls.Add(passwordTextBox);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            writer.AddFormRow("API Key:", "Please enter a valid SlideShare API key", apiKeyTextBox);
            writer.AddFormRow("Shared Secret:", "Please enter your SlideShare shared secret", sharedSecretTextBox);
            writer.AddFormRow("Username:", "Please enter your SlideShare username", usernameTextBox);
            writer.AddFormRow("Password:", "Please enter your SlideShare password", passwordTextBox);
        }
    }
}